﻿using System;
using System.Linq;
using Autofac;
using ComputerVision.BaseForm;
using ComputerVision.Common;
using ComputerVision.Data;
using ComputerVision.Services;
using ComputerVision.Services.User;
using QuanLyThongTinCoSo.Common;

namespace ComputerVision.PhieuDieuTraCoSoForm
{
    public partial class frmKiemTraAnToan : frmDetail
    {
        public Guid IdPhieu;
        private readonly IService<KiemTraAnToan> _kiemTraAnToanService;
        private readonly IService<CheDoKiemTra> _cheDoKiemTraService;
        private readonly IUserService<aspnet_Users> _userService;

        public frmKiemTraAnToan()
        {
            InitializeComponent();
            _kiemTraAnToanService = Program.Container.Resolve<IService<KiemTraAnToan>>();
            _userService = Program.Container.Resolve<IUserService<aspnet_Users>>();
            _cheDoKiemTraService = Program.Container.Resolve<IService<CheDoKiemTra>>();
            dtNgay.Focus();
            Text = @"Thêm Kiểm tra an toàn PCCC";
            LoadCheDoKiemTra();
        }

        public frmKiemTraAnToan(Guid id)
        {
            InitializeComponent();
            _kiemTraAnToanService = Program.Container.Resolve<IService<KiemTraAnToan>>();            
            _userService = Program.Container.Resolve<IUserService<aspnet_Users>>();
            _cheDoKiemTraService = Program.Container.Resolve<IService<CheDoKiemTra>>();
            Text = @"Sửa Kiểm tra an toàn PCCC";
            dtNgay.Focus();
            _fromtype = Constants.TypeForm.Edit;
            guid = id;

            LoadCheDoKiemTra();

            var item = _kiemTraAnToanService.Detail(id);
            if (item != null)
            {
                dtNgay.Value = item.Ngay;
                drlCheDoKiemTra.SelectedValue = item.CheDoKiemTraId;
                txtTonTai.Text = item.TonTai;
                txtKienNghi.Text = item.KienNghi;
            }            
        }

        public void LoadCheDoKiemTra()
        {
            drlCheDoKiemTra.DataSource = _cheDoKiemTraService.ToList().OrderBy(c => c.Ten).ToList();
            drlCheDoKiemTra.DisplayMember = "Ten";
            drlCheDoKiemTra.ValueMember = "Id";
        }

        #region Validation

        protected override bool Validator()
        {
            errorProvider.Dispose();

            if (string.IsNullOrEmpty(dtNgay.Text))
            {
                errorProvider.SetError(dtNgay, "Bạn chưa nhập thời gian. Xin vui lòng kiểm tra lại");
                return false;
            }

            if (drlCheDoKiemTra.SelectedValue == null)
            {
                errorProvider.SetError(drlCheDoKiemTra, "Bạn chưa nhập chế độ kiểm tra. Xin vui lòng kiểm tra lại");
                return false;
            }

            return true;
        }

        #endregion

        #region Insert
        protected override void Insert()
        {
            try
            {
                var model = new KiemTraAnToan()
                {
                    Id = Guid.NewGuid(),
                    Ngay = dtNgay.Value.Date,
                    CheDoKiemTraId = Guid.Parse(drlCheDoKiemTra.SelectedValue.ToString()),
                    TonTai = txtTonTai.Text,
                    KienNghi = txtKienNghi.Text,
                    PhieuDieuTraCoSoesId = this.IdPhieu,
                    Created = DateTime.Now,
                    CreatedBy = Constants.CurrentUser,
                    Modified = DateTime.Now,
                    ModifiedBy = Constants.CurrentUser
                };
                _kiemTraAnToanService.Create(model);
            }
            catch (Exception ex)
            {
                AlertList.Error("Lỗi phát sinh khi tạo mới dữ liệu.");
            }
        }
        #endregion

        #region Edit

        protected override void Edit()
        {
            var item = _kiemTraAnToanService.Detail(guid);
            if (item != null)
            {
                item.Ngay = dtNgay.Value.Date;
                item.CheDoKiemTraId = Guid.Parse(drlCheDoKiemTra.SelectedValue.ToString());
                item.TonTai = txtTonTai.Text;
                item.KienNghi = txtKienNghi.Text;
                item.Modified = DateTime.Now;
                item.ModifiedBy = Constants.CurrentUser;
                _kiemTraAnToanService.Update(item);
            }
        }

        #endregion


    }
}
